from . import app_rendering
from flask import render_template, request, redirect
from particular_authority.consumption_mysql import ConsumPtion
from conn_mysql import ConnectMysql
from particular_authority.consumption_notify import ConsumptionNotify
from token_mmwl import Token


@app_rendering.route('/add_consumption')

def consumption():
    
    get_params = request.args

    token = get_params.get('token')
    company_id = get_params.get('company_id')
    consumption_id = get_params.get('consumption_id')

    token_notify_result = Token().token_status(token)#验证token值有效性

    if token_notify_result['status'] == -1:

        return redirect('/login?msg=登录超时或没有登录为了您的账户安全请通重新登录')


    with ConsumptionNotify(company_id, consumption_id) as CN:

        result = CN.add_consumpation()

    if result['status'] == 1:

        return redirect(f'/personal?token={token}')

    else:

        return redirect(f"/detail/{consumption_id}?token={token}&msg={result['errorMsg']}")



